package com.zhongxi.guoyuan.view.rewardCoin.categoryPage;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.rewardCoin.RewardCoinAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/27.
 */

public class NewRewardItemPage extends BasePage implements PullToRefreshBase.OnRefreshListener2<GridView> {

    private String id;
    private PullToRefreshGridView lvList;
    private List<HotGoodsData> data = new ArrayList<>();
    private RewardCoinAdapter adapter;

    public NewRewardItemPage(Context ctx, String id) {
        super(ctx);
        this.id = id;
    }

    @Override
    public View initView() {
        View view = View.inflate(ctx, R.layout.item_reward_category_page, null);
        lvList = (PullToRefreshGridView) view.findViewById(R.id.lvList);
        lvList.setMode(PullToRefreshBase.Mode.BOTH);
        return view;
    }

    @Override
    public void initData() {
        adapter = new RewardCoinAdapter(ctx, data);
        GridView gridView = lvList.getRefreshableView();
        gridView.setAdapter(adapter);
        getGoodsList();
        TextView tv = new TextView(ctx);
        tv.setGravity(Gravity.CENTER);
        tv.setText("暂无数据,敬请期待");
        tv.setTextSize(20);
        //当界面为空的时候显示的视图
        lvList.setEmptyView(tv);
    }

    /**
     * 获取商品列表
     */
    private void getGoodsList() {
        String str = "&cateid=" + id + "&orderBy=0&page=" + pageNum;
        getServer(Url.MALL_GOODS_LIST, null, str, false, null, "get");
    }

    @Override
    public void initEvent() {
        lvList.setOnRefreshListener(this);
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ctx, RewardCoinDetailActivity.class);
                intent.putExtra("gid", data.get(position).getId());
                ctx.startActivity(intent);
            }
        });
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            HotGoodsResult result = gson.fromJson(response, HotGoodsResult.class);
            if (result.getSafeobj() != null && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if (pageNum == 1) {
                data.clear();
                adapter.notifyDataSetChanged();
            }
            if (null != result.getGoodsInfo() && result.getGoodsInfo().size() > 0) {
                data.addAll(result.getGoodsInfo());
            }
            adapter.notifyDataSetChanged();
            lvList.onRefreshComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            lvList.onRefreshComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
        pageNum = 1;
        getGoodsList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
        pageNum++;
        getGoodsList();
    }
}
